<!DOCTYPE html>
<html>

<head>

    {include file="Public/header"}
    <link href="__STATIC__/layout/css/gyq.css" rel="stylesheet">
    <link rel="stylesheet" href="__STATIC__/layout/css/demo.css" type="text/css">
    <style>
        .radio-inline input[type=radio] {
            margin-top: 4px;
        }
    </style>
</head>

<body style="color:#333;padding:3px;">
<!-- 标题 -->
<div>
    <form action="" class="form-inline">
        <table class="row-margin-top tab-content table-bordered table" border="1" style="margin-bottom:0">
            <tr>
                <td style="min-width: 140px;width: 140px"><label for="" class="form-label">类型<span
                        class="colorRed">*</span>：</label></td>
                <td colspan="3">
                    <input type="radio" checked name="kind" value="1" id="chengPin">
                    <label>成品</label>&nbsp;&nbsp;
                    <input type="radio" name="kind" value="2" id="sellwl">
                    <label>销售物料</label>
                </td>
            </tr>
            <tr>
                <td style="min-width: 140px;width: 140px"><label for="" class="form-label">订货产品<span
                        class="colorRed">*</span>：</label></td>
                <td>
                    <div style="width:80%;float:left;">
                        <div class="input-group" id="product_input">
                            <input type="text" class="form-control" id="product" placeholder="请输入关键字或空格查询">
                            <div class="input-group-btn">
                                <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                </ul>
                            </div>
                        </div>
                        <div id="product_queding" class="selectedOnlyOne" style="display: none;">
                            <input type="hidden" name="product">
                            <span class="inin"></span>
                            <span class="close">×</span>
                        </div>
                    </div>
                    <a href="javascript:;" style="float:left;line-height:30px;" id="selectdhcp">选择</a>
                </td>
                <td style="min-width: 140px;width: 140px"><label for="" class="form-label">订货数量<span
                        class="colorRed">*</span>：</label></td>
                <td><input type="text" class="form-control" name="num" id="num"></td>
            </tr>
            <tr>
                <td><label for="" class="form-label">单位：</label></td>
                <td><input type="hidden" name="unit" ><span id="unit"></span></td>
                <td><label for="" class="form-label">产品编码：</label></td>
                <td><input type="hidden" name="prod_sn"><span id="prod_sn"></span></td>
            </tr>
            <tr>
                <td><label for="" class="form-label">原供价(元)：</label></td>
                <td><input type="hidden" name="ygj"><span id="ygj"></span></td>
                <td><label for="" class="form-label">现供价(元) <span class="colorRed">*</span>：</label></td>
                <td><input type="text" class="form-control" name="xgj" value=""></td>
            </tr>
            <tr>
                <td><label for="" class="form-label">原金额(元)：</label></td>
                <td><input type="hidden" name="yje" ><span id="yje"></span></td>
                <td><label for="" class="form-label">应收金额(元)：</label></td>
                <td><input type="hidden" name="ysje"><span id="ysje"></span></td>
            </tr>
            <tr>
                <td style="min-width: 140px;width: 140px"><label for="" class="form-label">搭赠产品：</label></td>
                <td>
                    <div style="width:60%;float:left;">
                        <div class="input-group" id="dzcp_input">
                            <input type="text" class="form-control" id="dzcp" placeholder="请输入关键字或空格查询">
                            <div class="input-group-btn">
                                <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                </ul>
                            </div>
                        </div>
                        <div id="dzcp_queding" class="selectedOnlyOne" style="display: none;">
                            <input type="hidden" name="dzcp">
                            <span class="inin"></span>
                            <span class="close">×</span>
                        </div>
                    </div>
                    <a href="javascript:;" style="float:left;line-height:30px;" id="selectdzcp">选择</a>
                    <a href="javascript:;" style="float:left;line-height:30px;" id="tdhcp">&nbsp;同订货产品</a>
                </td>
                <td style="min-width: 140px;width: 140px"><label for="" class="form-label">搭赠数量：</label></td>
                <td><input type="text" class="form-control" name="dzsl" id="dzsl"></td>
            </tr>
            <tr>
                <td><label for="" class="form-label">单位：</label></td>
                <td><input type="hidden" name="dz_unit"><span id="dz_unit"></span></td>
                <td><label for="" class="form-label">产品编码：</label></td>
                <td><input type="hidden" name="dz_cpbm"><span id="dz_cpbm"></span></td>
            </tr>
            <tr>
                <td><label for="" class="form-label">原供价(元)：</label></td>
                <td><input type="hidden" name="dz_ygj"><span id="dz_ygj"></span></td>
                <td><label for="" class="form-label">现供价(元) <span class="colorRed">*</span>：</label></td>
                <td><input type="text" class="form-control" name="dz_xgj"></td>
            </tr>
            <tr>
                <td><label for="" class="form-label">原金额(元)：</label></td>
                <td><input type="hidden" name="dz_yje"><span id="dz_yje"></span></td>
                <td><label for="" class="form-label">应收金额(元)：</label></td>
                <td><input type="hidden" name="dz_ysje"><span id="dz_ysje"></span></td>
            </tr>
            
            <tr>
                <td>备注：</td>
                <td colspan="3"><input type="text" maxlength="50" class="form-control" style="width: 60%" name="beizhu"></td>
            </tr>
        </table>
        <div class="row sub_btn" style="width:100%;text-align:center;padding: 20px 0">
            <input type="button" class="btn btn-success save" id="save1" value="保存并再添加一条">
            <input type="button" class="btn btn-info save" id="save2" value="保存">
            <input type="button" class="btn btn-warning" value="返回" id="fanhui">
        </div>
    </form>
</div>
<!-- 全局js -->
{include file="Public/footer"}
<script src="__STATIC__/layout/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="__STATIC__/layout/js/plugins/suggest/bootstrap-suggest.min.js"></script>
<script>
    $(function () {
        var cusName = [];
        var xsryName = [];
        var cusName2 = [];

        $('[name=kind]').on('mousedown',function () {
            if($(this).prop('checked')==false){
                if($('[name=product]').val()!=''||$('[name=dzcp]').val()!=''){
                    swal('已选择产品，不能更换类型')
                    return false
                }
            }


            if($(this).val()==1){
                $('#product').bsSuggest('destroy')
                //订单产品搜索自动补全
                $("#product").bsSuggest({
                    url: '{:url("bs/product",["jxs_id"=>request()->get("jxs_id")])}',
                    effectiveFields: ["id", "pro_name"],
                    searchFields: [ "pro_name"],
                    effectiveFieldsAlias: {pro_name: "名称"},
                    ignorecase: true,
                    showHeader: true,
                    showBtn: false,     //不显示下拉按钮
                    delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
                    idField: "id",
                    keyField: "pro_name",
                    clearable: true
                }).on('onDataRequestSuccess', function (e, result) {
                    cusName = result.value
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword) {
                    $('#product_queding').find('.inin').html(keyword.key)
                    $('#product_queding').find('.inin').data('id',keyword.id)
                    $('#product_queding').show()
                    $('#product_input').hide()
                    $('#product_queding').find('input').val(keyword.id)
                    for(var i = 0;i<cusName.length;i++){
                        if(cusName[i].id==keyword.id){
                            $('[name=unit]').val(cusName[i].unit)
                            $('#unit').html(cusName[i].unit)
                            $('[name=prod_sn]').val(cusName[i].pro_no)
                            $('#prod_sn').html(cusName[i].pro_no)
                            $('[name=ygj]').val(cusName[i].lingshou_price)
                            $('#ygj').html(cusName[i].lingshou_price)
                            $('[name=xgj]').val(cusName[i].lingshou_price)
                            $('#xgj').html(cusName[i].lingshou_price)
                            $('[name=yje]').val(0)
                            $('#yje').html(0.0000)
                            $('[name=ysje]').val(0)
                            $('#ysje').html(0.0000)
                        }
                    }
                }).on('onUnsetSelectValue', function () {
                    console.log("onUnsetSelectValue");
                });
                //选择订货产品
                $('#selectdhcp').off('click')
                $('#selectdhcp').on('click',function(){
                    top.layer.open({
                        type: 2,
                        title: '&nbsp;',
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['800px', '500px'],
                        content: '{:url("open/product2",["jxs_id"=>request()->get("jxs_id")])}',
                        btn:['确定','关闭'],
                        yes:function (i,o) {
                            var a = $(o).find('iframe').contents()

                            var id =  a.find("#renyuan").val();
                            var name =  a.find("#renyuanname").val();
                            var unit =  a.find("#unit").val();
                            var pro_no =  a.find("#pro_no").val();
                            var fayun_price =  a.find("#lingshou_price").val();
                            if(id){
                                var jxs_queding = $('#product_queding')
                                var jxs_input = $('#product_input')
                                jxs_queding.find('.inin').html(name)
                                jxs_queding.find('.inin').data('id',id)
                                jxs_queding.show()
                                jxs_input.hide()
                                jxs_queding.find('input').val(id)

                                $('[name=unit]').val(unit)
                                $('#unit').html(unit)
                                $('[name=prod_sn]').val(pro_no)
                                $('#prod_sn').html(pro_no)
                                $('[name=ygj]').val(fayun_price)
                                $('#ygj').html(fayun_price)
                                $('[name=xgj]').val(fayun_price)
                                $('#xgj').html(fayun_price)
                                $('[name=yje]').val(0)
                                $('#yje').html(0.0000)
                                $('[name=ysje]').val(0)
                                $('#ysje').html(0.0000)
                                $('#num').val('')
                            }

                            top.layer.close(i)
                        }
                    })
                    return false
                })
                $('#dzcp').bsSuggest('destroy')
                $("#dzcp").bsSuggest({
                    url: '{:url("bs/product",["jxs_id"=>request()->get("jxs_id")])}',
                    effectiveFields: ["id", "pro_name"],
                    searchFields: [ "pro_name"],
                    effectiveFieldsAlias: {pro_name: "名称"},
                    ignorecase: true,
                    showHeader: true,
                    showBtn: false,     //不显示下拉按钮
                    delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
                    idField: "id",
                    keyField: "pro_name",
                    clearable: true
                }).on('onDataRequestSuccess', function (e, result) {
                    cusName2 = result.value
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword) {
                    $('#dzcp_queding').find('.inin').html(keyword.key)
                    $('#dzcp_queding').find('.inin').data('id',keyword.id)
                    $('#dzcp_queding').show()
                    $('#dzcp_input').hide()
                    $('#dzcp_queding').find('input').val(keyword.id)
                    for(var i = 0;i<cusName2.length;i++){
                        if(cusName2[i].id==keyword.id){
                            $('[name=dz_unit]').val(cusName2[i].unit)
                            $('#dz_unit').html(cusName2[i].unit)
                            $('[name=dz_cpbm]').val(cusName2[i].pro_no)
                            $('#dz_cpbm').html(cusName2[i].pro_no)
                            $('[name=dz_ygj]').val(cusName2[i].lingshou_price)
                            $('#dz_ygj').html(cusName2[i].lingshou_price)
                            $('[name=dz_xgj]').val(cusName2[i].lingshou_price)
                            $('#dz_xgj').html(cusName2[i].lingshou_price)
                            $('[name=dz_yje]').val(0)
                            $('#dz_yje').html(0.0000)
                            $('[name=dz_ysje]').val(0)
                            $('#dz_ysje').html(0.0000)
                        }
                    }
                });
                //选择搭赠产品
                $('#selectdzcp').off('click')
                $('#selectdzcp').on('click',function(){
                    top.layer.open({
                        type: 2,
                        title: '&nbsp;',
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['800px', '500px'],
                        content: '{:url("open/product2",["jxs_id"=>request()->get("jxs_id")])}',
                        btn:['确定','关闭'],
                        yes:function (i,o) {
                            var a = $(o).find('iframe').contents()

                            var id =  a.find("#renyuan").val();
                            var name =  a.find("#renyuanname").val();
                            var unit =  a.find("#unit").val();
                            var pro_no =  a.find("#pro_no").val();
                            var fayun_price =  a.find("#lingshou_price").val();
                            if(id){
                                var jxs_queding = $('#dzcp_queding')
                                var jxs_input = $('#dzcp_input')
                                jxs_queding.find('.inin').html(name)
                                jxs_queding.find('.inin').data('id',id)
                                jxs_queding.show()
                                jxs_input.hide()
                                jxs_queding.find('input').val(id)

                                $('[name=dz_unit]').val(unit)
                                $('#dz_unit').html(unit)
                                $('[name=dz_cpbm]').val(pro_no)
                                $('#dz_cpbm').html(pro_no)
                                $('[name=dz_ygj]').val(fayun_price)
                                $('#dz_ygj').html(fayun_price)
                                $('[name=dz_xgj]').val(fayun_price)
                                $('#dz_xgj').html(fayun_price)
                                $('[name=dz_yje]').val(0)
                                $('#dz_yje').html(0.0000)
                                $('[name=dz_ysje]').val(0)
                                $('#dz_ysje').html(0.0000)
                                $('#dzsl').val('')
                            }

                            top.layer.close(i)
                        }
                    })
                    return false
                })
            }else{
                $('#product').bsSuggest('destroy')
                //订单产品搜索自动补全
                $("#product").bsSuggest({
                    url: '{:url("bs/dd_xswl",["jxs_id"=>request()->get("jxs_id")])}',
                    effectiveFields: ["id", "pro_name"],
                    searchFields: [ "pro_name"],
                    effectiveFieldsAlias: {pro_name: "名称"},
                    ignorecase: true,
                    showHeader: true,
                    showBtn: false,     //不显示下拉按钮
                    delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
                    idField: "id",
                    keyField: "pro_name",
                    clearable: true
                }).on('onDataRequestSuccess', function (e, result) {
                    cusName = result.value
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword) {
                    $('#product_queding').find('.inin').html(keyword.key)
                    $('#product_queding').find('.inin').data('id',keyword.id)
                    $('#product_queding').show()
                    $('#product_input').hide()
                    $('#product_queding').find('input').val(keyword.id)
                    for(var i = 0;i<cusName.length;i++){
                        if(cusName[i].id==keyword.id){
                            $('[name=unit]').val(cusName[i].unit)
                            $('#unit').html(cusName[i].unit)
                            $('[name=prod_sn]').val(cusName[i].pro_no)
                            $('#prod_sn').html(cusName[i].pro_no)
                            $('[name=ygj]').val(cusName[i].lingshou_price)
                            $('#ygj').html(cusName[i].lingshou_price)
                            $('[name=xgj]').val(cusName[i].lingshou_price)
                            $('#xgj').html(cusName[i].lingshou_price)
                            $('[name=yje]').val(0)
                            $('#yje').html(0.0000)
                            $('[name=ysje]').val(0)
                            $('#ysje').html(0.0000)
                        }
                    }
                }).on('onUnsetSelectValue', function () {
                    console.log("onUnsetSelectValue");
                });
                $('#selectdhcp').off('click')
                $('#selectdhcp').on('click',function(){
                    top.layer.open({
                        type: 2,
                        title: '&nbsp;',
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['800px', '500px'],
                        content: '{:url("open/material2",["jxs_id"=>request()->get("jxs_id")])}',
                        btn:['确定','关闭'],
                        yes:function (i,o) {
                            var a = $(o).find('iframe').contents()

                            var id =  a.find("#renyuan").val();
                            var name =  a.find("#renyuanname").val();
                            var unit =  a.find("#unit").val();
                            var pro_no =  a.find("#pro_no").val();
                            var fayun_price =  a.find("#lingshou_price").val();
                            if(id){
                                var jxs_queding = $('#product_queding')
                                var jxs_input = $('#product_input')
                                jxs_queding.find('.inin').html(name)
                                jxs_queding.find('.inin').data('id',id)
                                jxs_queding.show()
                                jxs_input.hide()
                                jxs_queding.find('input').val(id)

                                $('[name=unit]').val(unit)
                                $('#unit').html(unit)
                                $('[name=prod_sn]').val(pro_no)
                                $('#prod_sn').html(pro_no)
                                $('[name=ygj]').val(fayun_price)
                                $('#ygj').html(fayun_price)
                                $('[name=xgj]').val(fayun_price)
                                $('#xgj').html(fayun_price)
                                $('[name=yje]').val(0)
                                $('#yje').html(0.0000)
                                $('[name=ysje]').val(0)
                                $('#ysje').html(0.0000)
                                $('#num').val('')
                            }

                            top.layer.close(i)
                        }
                    })
                    return false
                })
                $('#dzcp').bsSuggest('destroy')
                $("#dzcp").bsSuggest({
                    url: '{:url("bs/dd_xswl",["jxs_id"=>request()->get("jxs_id")])}',
                    effectiveFields: ["id", "pro_name"],
                    searchFields: [ "pro_name"],
                    effectiveFieldsAlias: {pro_name: "名称"},
                    ignorecase: true,
                    showHeader: true,
                    showBtn: false,     //不显示下拉按钮
                    delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
                    idField: "id",
                    keyField: "pro_name",
                    clearable: true
                }).on('onDataRequestSuccess', function (e, result) {
                    cusName2 = result.value
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword) {
                    $('#dzcp_queding').find('.inin').html(keyword.key)
                    $('#dzcp_queding').find('.inin').data('id',keyword.id)
                    $('#dzcp_queding').show()
                    $('#dzcp_input').hide()
                    $('#dzcp_queding').find('input').val(keyword.id)
                    for(var i = 0;i<cusName2.length;i++){
                        if(cusName2[i].id==keyword.id){
                            $('[name=dz_unit]').val(cusName2[i].unit)
                            $('#dz_unit').html(cusName2[i].unit)
                            $('[name=dz_cpbm]').val(cusName2[i].pro_no)
                            $('#dz_cpbm').html(cusName2[i].pro_no)
                            $('[name=dz_ygj]').val(cusName2[i].lingshou_price)
                            $('#dz_ygj').html(cusName2[i].lingshou_price)
                            $('[name=dz_xgj]').val(cusName2[i].lingshou_price)
                            $('#dz_xgj').html(cusName2[i].lingshou_price)
                            $('[name=dz_yje]').val(0)
                            $('#dz_yje').html(0.0000)
                            $('[name=dz_ysje]').val(0)
                            $('#dz_ysje').html(0.0000)
                        }
                    }
                });
                $('#selectdzcp').off('click')
                $('#selectdzcp').on('click',function(){
                    top.layer.open({
                        type: 2,
                        title: '&nbsp;',
                        shadeClose: true,
                        shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['800px', '500px'],
                        content: '{:url("open/material2",["jxs_id"=>request()->get("jxs_id")])}',
                        btn:['确定','关闭'],
                        yes:function (i,o) {
                            var a = $(o).find('iframe').contents()

                            var id =  a.find("#renyuan").val();
                            var name =  a.find("#renyuanname").val();
                            var unit =  a.find("#unit").val();
                            var pro_no =  a.find("#pro_no").val();
                            var fayun_price =  a.find("#lingshou_price").val();
                            if(id){
                                var jxs_queding = $('#dzcp_queding')
                                var jxs_input = $('#dzcp_input')
                                jxs_queding.find('.inin').html(name)
                                jxs_queding.find('.inin').data('id',id)
                                jxs_queding.show()
                                jxs_input.hide()
                                jxs_queding.find('input').val(id)

                                $('[name=dz_unit]').val(unit)
                                $('#dz_unit').html(unit)
                                $('[name=dz_cpbm]').val(pro_no)
                                $('#dz_cpbm').html(pro_no)
                                $('[name=dz_ygj]').val(fayun_price)
                                $('#dz_ygj').html(fayun_price)
                                $('[name=dz_xgj]').val(fayun_price)
                                $('#dz_xgj').html(fayun_price)
                                $('[name=dz_yje]').val(0)
                                $('#dz_yje').html(0.0000)
                                $('[name=dz_ysje]').val(0)
                                $('#dz_ysje').html(0.0000)
                                $('#dzsl').val('')
                            }

                            top.layer.close(i)
                        }
                    })
                    return false
                })
            }

        })

        $('.save').on('click',function () {
            var form = $(this).closest('form')
            var type = $(this).prop('id')
            var pform = parent.$('#form1').serialize()

            $.post('',form.serialize()+'&order_id='+parent.$('#id').val()+'&'+pform,function (data) {
                if(data.success){
                    parent.$('#id').val(data.id)
                    swal({title:data.msg},function () {
                        var oid=data.id;
                        var qid=parent.$("[name='yhPolicy']").val();
                        if(oid!=""&&qid!=""){
                            $.post('{:url("qdyhzcxz")}',{qdyh_id:qid,order_id:oid})
                        }
                        if(type=='save1'){
                            location.reload()
                        }else{
                            parent.location.href='{:url("updateorder")}?oid='+data.id
//                            parent.layer.close(parent.layer.getFrameIndex(window.name))
                        }
                    })
                }else{
                    swal(data.msg,'','error')
                }
            },'json')
        })

        $('#fanhui').on('click',function () {
            var oid=parent.$('#id').val()
            if(oid==""){
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }else{
                parent.location.href='{:url("updateorder")}?oid='+oid
            }
            
        })

        //选择销售人员
        $('#selectxsry').on('click',function(){
            top.layer.open({
                type: 2,
                title: '&nbsp;',
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '500px'],
                content: '{:url("open/employee2")}',
                btn:['确定','关闭'],
                yes:function (i,o) {
                    var a = $(o).find('iframe').contents()

                    var id =  a.find("#renyuan").val();
                    var name =  a.find("#renyuanname").val();
                    var ssbm_id = a.find('#ssbm_id').val();
                    var ssbm = a.find('#ssbm').val();
                    if(id){
                        var jxs_queding = $('#xsry_queding')
                        var jxs_input = $('#xsry_input')

                        jxs_queding.find('.inin').html(name)
                        jxs_queding.find('.inin').data('id',id)
                        jxs_queding.show()
                        jxs_queding.find('input').val(id)
                        jxs_input.hide()
                        $('[name=ssbm]').val(ssbm_id)
                        $('#ssbm').html(ssbm)
                    }

                    top.layer.close(i)
                }
            })
            return false
        })

        $("#xsry").bsSuggest({
            url: '{:url("bs/xsry",["jxs_id"=>request()->get("jxs_id")])}',
            effectiveFields: ["id", "em_name"],
            searchFields: [ "em_name"],
            effectiveFieldsAlias: {em_name: "姓名"},
            ignorecase: true,
            showHeader: true,
            showBtn: false,     //不显示下拉按钮
            delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
            idField: "id",
            keyField: "em_name",
            clearable: true
        }).on('onDataRequestSuccess', function (e, result) {
            xsryName = result.value
            console.log('onDataRequestSuccess: ', result);
        }).on('onSetSelectValue', function (e, keyword) {
            $('#xsry_queding').find('.inin').html(keyword.key)
            $('#xsry_queding').find('.inin').data('id',keyword.id)
            $('#xsry_queding').show()
            $('#xsry_input').hide()
            $('#xsry_queding').find('input').val(keyword.id)
            for(var i = 0;i<xsryName.length;i++){
                if(xsryName[i].id==keyword.id){
                    $('[name=ssbm]').val(xsryName[i].ssbm_id)
                    $('#ssbm').html(xsryName[i].ssbm)
                }
            }
        });

        //关闭销售人员选择
        $('#xsry_queding .close').click(function () {
            $(this).closest('#xsry_queding').hide().find('.inin').html('')
            $(this).closest('#xsry_queding').hide().find('input').val('')
            $('#xsry_input').show().find('input').val('')
            $('#ssbm').html('')
            $('[name=ssbm]').val('')
        })

        //同订货产品
        $('#tdhcp').on('click',function () {
            var product = $('[name=product]').val()
            if(product==''){
                swal('请先选择订货产品')
                return false
            }
            $('#dzcp').val(product)
            $('[name=dzcp]').val(product)
            $('#dzcp_input').hide()
            $('#dzcp_queding .inin').html($('#product_queding .inin').html())
            $('#dzcp_queding').show()
            $('[name=dz_unit]').val($('[name=unit]').val())
            $('#dz_unit').html($('[name=unit]').val())
            $('[name=dz_cpbm]').val($('[name=prod_sn]').val())
            $('#dz_cpbm').html($('[name=prod_sn]').val())
            $('[name=dz_ygj]').val($('[name=ygj]').val())
            $('#dz_ygj').html($('[name=ygj]').val())
            $('[name=dz_xgj]').val($('[name=ygj]').val())
            $('#dz_xgj').html($('[name=ygj]').val())
            $('[name=dz_yje]').val($('[name=yje]').val())
            $('#dz_yje').html($('[name=yje]').val())
            $('[name=dz_ysje]').val($('[name=ysje]').val())
            $('#dz_ysje').html($('[name=ysje]').val())
        })

        //选择订货产品
        $('#selectdhcp').on('click',function(){
            top.layer.open({
                type: 2,
                title: '&nbsp;',
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '500px'],
                content: '{:url("open/product2",["jxs_id"=>request()->get("jxs_id")])}',
                btn:['确定','关闭'],
                yes:function (i,o) {
                    var a = $(o).find('iframe').contents()

                    var id =  a.find("#renyuan").val();
                    var name =  a.find("#renyuanname").val();
                    var unit =  a.find("#unit").val();
                    var pro_no =  a.find("#pro_no").val();
                    var fayun_price =  a.find("#lingshou_price").val();
                    if(id){
                        var jxs_queding = $('#product_queding')
                        var jxs_input = $('#product_input')
                        jxs_queding.find('.inin').html(name)
                        jxs_queding.find('.inin').data('id',id)
                        jxs_queding.show()
                        jxs_input.hide()
                        jxs_queding.find('input').val(id)

                        $('[name=unit]').val(unit)
                        $('#unit').html(unit)
                        $('[name=prod_sn]').val(pro_no)
                        $('#prod_sn').html(pro_no)
                        $('[name=ygj]').val(fayun_price)
                        $('#ygj').html(fayun_price)
                        $('[name=xgj]').val(fayun_price)
                        $('#xgj').html(fayun_price)
                        $('[name=yje]').val(0)
                        $('#yje').html(0.0000)
                        $('[name=ysje]').val(0)
                        $('#ysje').html(0.0000)
                        $('#num').val('')
                    }

                    top.layer.close(i)
                }
            })
            return false
        })
        //选择搭赠产品
        $('#selectdzcp').on('click',function(){
            top.layer.open({
                type: 2,
                title: '&nbsp;',
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '500px'],
                content: '{:url("open/product2",["jxs_id"=>request()->get("jxs_id")])}',
                btn:['确定','关闭'],
                yes:function (i,o) {
                    var a = $(o).find('iframe').contents()

                    var id =  a.find("#renyuan").val();
                    var name =  a.find("#renyuanname").val();
                    var unit =  a.find("#unit").val();
                    var pro_no =  a.find("#pro_no").val();
                    var fayun_price =  a.find("#lingshou_price").val();
                    if(id){
                        var jxs_queding = $('#dzcp_queding')
                        var jxs_input = $('#dzcp_input')
                        jxs_queding.find('.inin').html(name)
                        jxs_queding.find('.inin').data('id',id)
                        jxs_queding.show()
                        jxs_input.hide()
                        jxs_queding.find('input').val(id)

                        $('[name=dz_unit]').val(unit)
                        $('#dz_unit').html(unit)
                        $('[name=dz_cpbm]').val(pro_no)
                        $('#dz_cpbm').html(pro_no)
                        $('[name=dz_ygj]').val(fayun_price)
                        $('#dz_ygj').html(fayun_price)
                        $('[name=dz_xgj]').val(fayun_price)
                        $('#dz_xgj').html(fayun_price)
                        $('[name=dz_yje]').val(0)
                        $('#dz_yje').html(0.0000)
                        $('[name=dz_ysje]').val(0)
                        $('#dz_ysje').html(0.0000)
                        $('#dzsl').val('')
                    }

                    top.layer.close(i)
                }
            })
            return false
        })
        $('#num').on('input',function () {
            var num = $(this).val()
            var ygj = $('input[name=ygj]').val()
            var yje = parseFloat(num)*parseFloat(ygj)
            $('#yje').html(yje)
            $('input[name=yje]').val(yje)
            var xgj = $('input[name=xgj]').val()
            var ysje = parseFloat(xgj)*parseFloat(num)
            $('input[name=ysje]').val(ysje)
            $('#ysje').html(ysje)
        })
        $('[name=xgj]').on('input',function () {
            var num = $('#num').val()
            var xgj = $('input[name=xgj]').val()
            var ysje = parseFloat(xgj)*parseFloat(num)
            $('input[name=ysje]').val(ysje)
            $('#ysje').html(ysje)
        })
        //搭赠数量
        $('#dzsl').on('input',function () {
            var num = $(this).val()
            var ygj = $('input[name=dz_ygj]').val()
            var yje = parseFloat(num)*parseFloat(ygj)
            var xgj = $('input[name=dz_xgj]').val()
            var ysje = 0
           /* {if isset($estiGiftCost)}
            if(ysje>{$estiGiftCost}){
                swal('应收金额不能超过渠道优惠政策预计搭赠产生的费用{$estiGiftCost}元')
                $(this).val('')
                return false
            }
            {/if}*/
            $('#dz_yje').html(yje)
            $('input[name=dz_yje]').val(yje)
            $('input[name=dz_ysje]').val(ysje)
            $('#dz_ysje').html(ysje)
        })
        $('[name=dz_xgj]').on('input',function () {
            var num = $('#dzsl').val()
            var xgj = $('input[name=dz_xgj]').val()
            var ysje = parseFloat(xgj)*parseFloat(num)
            $('input[name=dz_ysje]').val(ysje)
            $('#dz_ysje').html(ysje)
        })
        //订单产品搜索自动补全
        $("#product").bsSuggest({
            url: '{:url("bs/product",["jxs_id"=>request()->get("jxs_id")])}',
            effectiveFields: ["id", "pro_name"],
            searchFields: [ "pro_name"],
            effectiveFieldsAlias: {pro_name: "名称"},
            ignorecase: true,
            showHeader: true,
            showBtn: false,     //不显示下拉按钮
            delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
            idField: "id",
            keyField: "pro_name",
            clearable: true
        }).on('onDataRequestSuccess', function (e, result) {
            cusName = result.value
            console.log('onDataRequestSuccess: ', result);
        }).on('onSetSelectValue', function (e, keyword) {
            $('#product_queding').find('.inin').html(keyword.key)
            $('#product_queding').find('.inin').data('id',keyword.id)
            $('#product_queding').show()
            $('#product_input').hide()
            $('#product_queding').find('input').val(keyword.id)
            for(var i = 0;i<cusName.length;i++){
                if(cusName[i].id==keyword.id){
                    $('[name=unit]').val(cusName[i].unit)
                    $('#unit').html(cusName[i].unit)
                    $('[name=prod_sn]').val(cusName[i].pro_no)
                    $('#prod_sn').html(cusName[i].pro_no)
                    $('[name=ygj]').val(cusName[i].lingshou_price)
                    $('#ygj').html(cusName[i].lingshou_price)
                    $('[name=xgj]').val(cusName[i].lingshou_price)
                    $('#xgj').html(cusName[i].lingshou_price)
                    $('[name=yje]').val(0)
                    $('#yje').html(0.0000)
                    $('[name=ysje]').val(0)
                    $('#ysje').html(0.0000)
                }
            }
        }).on('onUnsetSelectValue', function () {
            console.log("onUnsetSelectValue");
        });

        //搭赠产品
        $("#dzcp").bsSuggest({
            url: '{:url("bs/product",["jxs_id"=>request()->get("jxs_id")])}',
            effectiveFields: ["id", "pro_name"],
            searchFields: [ "pro_name"],
            effectiveFieldsAlias: {pro_name: "名称"},
            ignorecase: true,
            showHeader: true,
            showBtn: false,     //不显示下拉按钮
            delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
            idField: "id",
            keyField: "pro_name",
            clearable: true
        }).on('onDataRequestSuccess', function (e, result) {
            cusName2 = result.value
            console.log('onDataRequestSuccess: ', result);
        }).on('onSetSelectValue', function (e, keyword) {
            $('#dzcp_queding').find('.inin').html(keyword.key)
            $('#dzcp_queding').find('.inin').data('id',keyword.id)
            $('#dzcp_queding').show()
            $('#dzcp_input').hide()
            $('#dzcp_queding').find('input').val(keyword.id)
            for(var i = 0;i<cusName2.length;i++){
                if(cusName2[i].id==keyword.id){
                    $('[name=dz_unit]').val(cusName2[i].unit)
                    $('#dz_unit').html(cusName2[i].unit)
                    $('[name=dz_cpbm]').val(cusName2[i].pro_no)
                    $('#dz_cpbm').html(cusName2[i].pro_no)
                    $('[name=dz_ygj]').val(cusName2[i].lingshou_price)
                    $('#dz_ygj').html(cusName2[i].lingshou_price)
                    $('[name=dz_xgj]').val(cusName2[i].lingshou_price)
                    $('#dz_xgj').html(cusName2[i].lingshou_price)
                    $('[name=dz_yje]').val(0)
                    $('#dz_yje').html(0.0000)
                    $('[name=dz_ysje]').val(0)
                    $('#dz_ysje').html(0.0000)
                }
            }
        });

        //关闭经销商选择
        $('#product_queding .close').click(function () {
            $(this).closest('#product_queding').hide().find('.inin').html('')
            $(this).closest('#product_queding').hide().find('input').val('')
            $('#product_input').show().find('input').val('')
            $('#num').val('')
            $('[name=unit]').val('')
            $('#unit').html('')
            $('[name=prod_sn]').val('')
            $('#prod_sn').html('')
            $('[name=ygj]').val('')
            $('#ygj').html('')
            $('[name=xgj]').val('')
            $('[name=yje]').val('')
            $('#yje').html('')
            $('[name=ysje]').val('')
            $('#ysje').html('')
        })

        //关闭搭赠产品选择
        $('#dzcp_queding .close').click(function () {
            $(this).closest('#dzcp_queding').hide().find('.inin').html('')
            $(this).closest('#dzcp_queding').hide().find('input').val('')
            $('#dzcp_input').show().find('input').val('')
            $('#dzsl').val('')
            $('[name=dz_unit]').val('')
            $('#dz_unit').html('')
            $('[name=dz_cpbm]').val('')
            $('#dz_cpbm').html('')
            $('[name=dz_ygj]').val('')
            $('#dz_ygj').html('')
            $('[name=dz_xgj]').val('')
            $('[name=dz_yje]').val('')
            $('#dz_yje').html('')
            $('[name=dz_ysje]').val('')
            $('#dz_ysje').html('')
        })

        //弹出经销商树
        $('#selectCus').click(function () {
            layer.open({
                type:2,
                area:['60%','60%'],
                maxmin:true,
                content:link2
            })
            return  false
        })
    })
    function back() {
        history.go(-1)
    }

    var AgencyName = $("#AgencyName");
    sousuo(AgencyName)

    function sousuo(id) {
        id.bsSuggest({
            allowNoKeyword: false, //是否允许无关键字时请求数据
            multiWord: true, //以分隔符号分割的多关键字支持
            separator: ",", //多关键字支持时的分隔符，默认为空格
            getDataMethod: "url", //获取数据的方式，总是从 URL 获取
            url: 'http://unionsug.baidu.com/su?p=3&t=' + (new Date()).getTime() + '&wd=',
            /*优先从url ajax 请求 json 帮助数据，注意最后一个参数为关键字请求参数*/
            jsonp: 'cb',
            /*如果从 url 获取数据，并且需要跨域，则该参数必须设置*/
            processData: function (json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
                var i, len, data = {
                    value: []
                };
                if (!json || !json.s || json.s.length === 0) {
                    return false;
                }

                // console.log(json);
                len = json.s.length;

                jsonStr = "{'value':[";
                for (i = 0; i < len; i++) {
                    data.value.push({
                        word: json.s[i]
                    });
                }
                data.defaults = 'baidu';

                //字符串转化为 js 对象
                return data;
            }
        });

    }
</script>
</body>
</html>
